// 全局注册按钮级别权限组件
import { Auth } from "@/components/ReAuth";
// 自定义指令
import * as directives from "@/directives";
import { setupStore } from "@/store";
// import { useEcharts } from "@/plugins/echarts";
import { injectResponsiveStorage } from "@/utils/responsive";
import addNode from "@/views/workflow/com/components/addNode.vue";
import nodeWrap from "@/views/workflow/com/components/nodeWrap.vue";
import Table from "@pureadmin/table";

import { MotionPlugin } from "@vueuse/motion";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import { createApp, Directive } from "vue";
import App from "./App.vue";
import "./assets/iconfont/iconfont.css";
// 导入字体图标
import "./assets/iconfont/iconfont.js";
// 全局注册`@iconify/vue`图标库
import {
  FontIcon,
  IconifyIconOffline,
  IconifyIconOnline
} from "./components/ReIcon";
import { getServerConfig } from "./config";
import router from "./router";
// 导入公共样式
import "./style/index.scss";
// import PureDescriptions from "@pureadmin/descriptions";
// 引入重置样式
import "./style/reset.scss";
// 一定要在main.ts中导入tailwind.css，防止vite每次hmr都会请求src/style/index.scss整体css文件导致热更新慢的问题
import "./style/tailwind.css";
// 注册所有icon
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
/* 表单 */
import "@/iconfont/iconfont.css";
import Draggable from "@/lib/vuedraggable/vuedraggable.umd.js";
import { registerIcon } from "@/utils/el-icons";
import "virtual:svg-icons-register";

import ContainerWidgets from "@/components/form-designer/form-widget/container-widget/index";
import ContainerItems from "@/components/form-render/container-item/index";
import VFormRender from "@/components/form-render/index.vue";

import { addDirective } from "@/utils/directive";
import { loadExtension } from "@/extension/extension-loader";

import { QuillEditor } from "@vueup/vue-quill";
import "@vueup/vue-quill/dist/vue-quill.snow.css";
// import publicfunc from "@/utils/publicfunc";

const app = createApp(App);

Object.keys(directives).forEach(key => {
  app.directive(key, (directives as { [key: string]: Directive })[key]);
});
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

app.component("IconifyIconOffline", IconifyIconOffline);
app.component("IconifyIconOnline", IconifyIconOnline);
app.component("FontIcon", FontIcon);

app.component("Auth", Auth);

app.component("nodeWrap", nodeWrap); //初始化组件
app.component("addNode", addNode); //初始化组件
app.component("QuillEditor", QuillEditor);
// app.use(publicfunc);

getServerConfig(app).then(async config => {
  app.use(router);
  await router.isReady();
  injectResponsiveStorage(app, config);
  setupStore(app);
  app
    .use(MotionPlugin)
    .use(ElementPlus)
    // .use(useEcharts);
    .use(Table);

  // .use(PureDescriptions);
  app.mount("#app");
});

app.directive("enterNumber", {
  mounted(el, { value = 100 }, vnode) {
    el = el.nodeName == "INPUT" ? el : el.children[0];
    const RegStr =
      value == 0
        ? `^[\\+\\-]?\\d+\\d{0,0}`
        : `^[\\+\\-]?\\d+\\.?\\d{0,${value}}`;
    el.addEventListener("input", function () {
      el.value = el.value.match(new RegExp(RegStr, "g"));
      el.dispatchEvent(new Event("input"));
    });
  }
});
app.directive("focus", {
  mounted(el) {
    el.focus();
  }
});
/* 表单 */
registerIcon(app);
app.component("draggable", Draggable);
addDirective(app);

app.use(ContainerWidgets);
app.use(ContainerItems);
app.component("VFormRender", VFormRender);
loadExtension(app);
